/*================================================================
 *   Copyright (C) 2014 All rights reserved.
 *
 *   文件名称：ClientConn.cpp
 *   创 建 者：Zhang Yuanhao
 *   邮    箱：bluefoxah@gmail.com
 *   创建日期：2014年12月30日
 *   描    述：
 *
 ================================================================*/
#include "ClientConn.h"
#include "ImPduClient.h"


ClientConn::ClientConn():
m_bOpen(false)
{
}

ClientConn::~ClientConn()
{

}

net_handle_t ClientConn::connect(const string& strIp, uint16_t nPort)
{
	m_handle = netlib_connect(strIp.c_str(), nPort, imconn_callback, NULL);
    return  m_handle;
}



void ClientConn::OnConfirm()
{
    log("connected.\n");
}

void ClientConn::OnClose()
{
    log("onclose from handle=%d\n", m_handle);
    Close();
}

void ClientConn::OnTimer(uint64_t curr_tick)
{
    if (curr_tick > m_last_recv_tick + CLIENT_TIMEOUT) {
        log("conn to msg_server timeout\n");
        Close();
    }
}


void ClientConn::login(uint32_t nUserId)
{
    CImPduLoginReq cPdu(nUserId, CLIENT_TYPE_WIN);
    SendPdu(&cPdu);
}

void ClientConn::getOnlineUser()
{
    CImPduOnlineUserReq cPdu;
    SendPdu(&cPdu);
}

void ClientConn::initCall(uint32_t nPeerId, uint32_t nCallId)
{
    string strBody("hello");
    CImPduInitCallReq cPdu(nPeerId, nCallId, strBody.data(), strBody.length());
    SendPdu(&cPdu);
}

void ClientConn::hangupCall(uint32_t nPeerId, uint32_t nCallId)
{
    CImPduHungUpReq cPdu(nPeerId, nCallId);
    SendPdu(&cPdu);
}


void ClientConn::Close()
{
	if (m_handle != NETLIB_INVALID_HANDLE) {
		netlib_close(m_handle);
	}
	ReleaseRef();
}

void ClientConn::HandlePdu(CImPdu* pPdu)
{
    printf("pdu type = %u\n", pPdu->GetPduType());
	switch (pPdu->GetCommandId()) {
        case IM_PDU_LOGIN_RES:
            HandleLoginRes(pPdu);
            break;
        case IM_PDU_ONLINE_USER_RES:
            HandleOnlineUserRes(pPdu);
            break;
        case IM_PDU_INIT_CALL_RES:
            HandleInitCallRes(pPdu);
            break;
        case IM_PDU_HUNG_UP_RES:
            HandleHungUp(pPdu);
            break;
        default:
		log("wrong msg_type=%d\n", pPdu->GetCommandId());
		break;
	}
}

void ClientConn::HandleLoginRes(CImPdu *pPdu)
{
    CImPduLoginRes* pPduRes = (CImPduLoginRes*)pPdu;
    log("HandleLoginRes. status=%u\n", pPduRes->getStatus());
}

void ClientConn::HandleOnlineUserRes(CImPdu *pPdu)
{
    CImPduOnlineUserRes* pPduRes = (CImPduOnlineUserRes*) pPdu;
    log("HandleOnlineUserRes. cnt=%u", pPduRes->getOnlineUserCnt());
    m_lsOnlineUsers = pPduRes->getOnlieUsers();
    for (auto it=m_lsOnlineUsers.begin(); it!=m_lsOnlineUsers.end(); ++it) {
        log("%d\n", *it);
    }
}

void ClientConn::HandleInitCallRes(CImPdu *pPdu)
{
    CImPduInitCallRes* pPduRes = (CImPduInitCallRes*)pPdu;
    log("HandleInitCallRes. peer_id=%u, call_id=%u, code=%u, reason=%s\n", pPduRes->getToId(), pPduRes->getCallId(), pPduRes->getCode(), pPduRes->getReason().c_str());
}

void ClientConn::HandleHungUp(CImPdu *pPdu)
{
    CImPduHungUpRes* pPduRes = (CImPduHungUpRes*)pPdu;
    log("HandleHungUp. peer_id=%u\n", pPduRes->getPeerId());
}




